//
//  MainViewController.swift
//  weibo
//
//  Created by shixiaoyang on 16/4/7.
//  Copyright © 2016年 shixiaoyang. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()


        addChildViewControllers()
        setupComposedButton()
        
        
    }
    //MARK: -懒加载控件
    ///撰写按钮
    private lazy var composedButton: UIButton = UIButton(imageName: "tabbar_compose_icon_add", backImageName: "tabbar_compose_button")

    // 设置按钮的位置
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        tabBar.bringSubviewToFront(composedButton)
    }

    /// 设置撰写按钮
    private func setupComposedButton() {
        tabBar.addSubview(composedButton)
        
        let count = childViewControllers.count
        let w = tabBar.bounds.width / CGFloat(count) - 1
        
        composedButton.frame = CGRectInset(tabBar.bounds, 2 * w, 0)
        
        //添加按钮的监听方法
        composedButton.addTarget(self, action: "clickComposedButton", forControlEvents: .TouchUpInside)
    }
    
    //MARK: - 监听方法
    @objc private func clickComposedButton() {
    print("点击了撰写按钮")
    }
    
    //
    private func addChildViewController(vc: UIViewController,title: String, imageName: String) {
    
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        
        addChildViewController(UINavigationController(rootViewController: vc))
    }
    
    
    // 添加所有子控制器
    private func addChildViewControllers() {
        tabBar.tintColor = UIColor.orangeColor()
        
        addChildViewController(HomeViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageViewController(), title: "消息", imageName: "tabbar_message_center")
        
        addChildViewController(UIViewController())
        
        addChildViewController(DiscoverViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileViewController(), title: "我", imageName: "tabbar_profile")
    }
}
